package com.keyue.common.utils;

import com.alibaba.fastjson.JSON;
import com.keyue.common.exception.BusinessException;
import com.keyue.domain.UserInfoRedisVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName UserUtils
 * @Description 用户会话信息管理类
 * @Author liuyilin
 * @Date 2021/10/30 15:21
 * @Version 1.0
 */
@Component("userUtils")
@Slf4j
public class UserUtils {


    @Resource
    private RedisUtil redisUtil;

    /**
     * 获取当前登录用户的AccountNum
     *
     * @return
     */
    public String getCurrentUserAccountNum() {
        UserInfoRedisVO currentUserInfo = getCurrentUserInfo();
        if (null == currentUserInfo) {
            return null;
        }
        return currentUserInfo.getUserName();
    }

    public String getCurrentUserAccountNumNotThrowEx() {
        try {
            UserInfoRedisVO currentUserInfo = getCurrentUserInfo();
            if (null == currentUserInfo) {
                return null;
            }
            return currentUserInfo.getUserName();
        } catch (Exception e) {
            log.error("异常", e);
        }
        return "null";
    }

    public boolean isNewDataPermissionSwitch(){
        return StringUtils.isNotBlank(SecurityUtils.getUsername());
    }

    /**
     * 获取当前登录的用户信息
     *
     * @param
     * @return
     */
    public UserInfoRedisVO getCurrentUserInfo() {
        String userName = SecurityUtils.getUsername();
        UserInfoRedisVO currentUserInfo = getCurrentUserInfo(userName);
        if (currentUserInfo == null) {
            throw new BusinessException("未登录");
        }
        return currentUserInfo;
    }

    /**
     * 获取不到登录用户信息不会抛出异常，会返回null
     *
     * @return
     */
    public UserInfoRedisVO getCurrentUserInfoOfNullable() {
        String userName = SecurityUtils.getUsername();
        return getCurrentUserInfo(userName);
    }

    /**
     * 根据用户账号获取用户信息
     *
     * @param userName
     * @return
     */
    public UserInfoRedisVO getCurrentUserInfo(String userName) {
        String userCache = redisUtil.get(getUserInfoCacheKey(userName));
        if (StringUtils.isBlank(userCache)) {
            return null;
        }
        UserInfoRedisVO userInfoRedisVO = JSON.parseObject(userCache, UserInfoRedisVO.class);
        return userInfoRedisVO;
    }

    public UserInfoRedisVO getUserInfoByKey(String cacheKey) {
        String userCache = redisUtil.get(cacheKey);
        UserInfoRedisVO userInfoRedisVO = JSON.parseObject(userCache, UserInfoRedisVO.class);
        return userInfoRedisVO;
    }


    /**
     * 清楚用户缓存
     */
    public void logout() {
        String userName = SecurityUtils.getUsername();
        redisUtil.delete(getUserInfoCacheKey(userName));
    }

    public void cacheUserLoginInfo(UserInfoRedisVO userInfo) {
        cacheUserInfo(userInfo);
    }



    private void cacheUserInfo(UserInfoRedisVO userInfo) {
        //用户缓存
        log.info("用户登录，生成用户缓存信息:{}", JSON.toJSONString(userInfo));
        String userCacheKey = getUserInfoCacheKey(userInfo.getUserName());
        redisUtil.setEx(userCacheKey, JSON.toJSONString(userInfo), 12, TimeUnit.HOURS);
    }

    public static String getUserInfoCacheKey(String userName) {
        return "system_manage_" + userName;
    }

}